<style scoped>
.form-wrap{
	width:800px;margin:0 auto;
}
</style>
<template>
	<div class="page-setting">
		<div class="crumbs">
			<router-link to="/">首页</router-link> / <span>消防档案</span> / <span class="active">企业单位档案</span>
		</div>
		<el-container :style="{height: mainH+'px'}">
		  	<el-main class="census-main">
		  		<div class="form-wrap">
		  			<New
						ref="newData"
						:unitList="unitList"
						:levelList="levelList"
						:industryList="industryList"
			    		:activeData="detail"
			    		@success="submitSuccessAction"
			    		@fail="adding = false"
			    		@submitting="adding = true"
						type="fire"
			    		></New>
			    	<div class="text-right mt-30">
			    		<el-button v-if="hasPermission('029001')" style="width:100px;" type="primary" :loading="adding" @click="submitAction">提交数据</el-button>
			    	</div>
			  	</div>	
		  	</el-main>
		</el-container>
	</div>
</template>
<script>
import New from '@/components/census/record/RecordNew.vue'
export default {
	name: 'Record',
	components: {
		New
	},
	props: {
		winH: {
			type: Number,
			default: 500
		}
	},
	data () {
		return {
			detail: {},
			unitList: [],
			levelList: [],
			industryList: [],
			adding: false
		}
	},
	watch: {},
	computed: {
		mainH () {
			return this.winH-219
		}
	},
	methods: {
		getData () {
			this.$get('/business/business/detail').then(res => {
            	this.closeFullLoading()
            	let thisData = res.data
            	let thisIndustry = this.getSelectedIndustryById(this.industryList,thisData.industry)
            	thisData.industryName = thisIndustry ? thisIndustry.name : ''
            	this.detail = thisData
            }, (err) =>{
            	this.closeFullLoading()
            	if(err.msg){
					this.$message.error(err.msg);
				}
            })
		},
		getDict (groupname,cbk) {
			this.$get('/business/business/dictList', {
            	groupname: groupname
            }).then(res => {
            	cbk && cbk(res.data)
            }, (err) =>{
            	this.loading = false
				if(err.msg){
					this.$message.error(err.msg);
				}
            })
		},
		getIndustry (cbk) {
			this.$get('/business/business/industryList').then(res => {
				this.industryList = res.data
				cbk && cbk()
			}, (err) =>{
				this.closeFullLoading()
				if(err.msg){
					this.$message.error(err.msg);
				}
			})
		},
		getSelectedIndustryById (data,id) {
			let temp = null
			for(let i=0;i<data.length;i++){
				if(data[i].id==id){
					temp = data[i]
				}else if(data[i].industrys && data[i].industrys.length>0){
					temp = this.getSelectedIndustryById(data[i].industrys,id)
				}
				if(temp) break
			}
			return temp
		},
		submitAction() {
			this.$refs.newData.submitAction()
		},
		submitSuccessAction () {
			this.adding = false
		}
	},
	created () {
		this.openFullLoading()
		this.getIndustry(()=>{
			this.getData()
		})
		this.getDict('单位性质', data => {
			this.unitList = data
			this.unitObj = data.map(item=>{
				let temp = {}
				temp[item.id] = item.name
				return temp
			})
		})
		this.getDict('消防等级', data => {
			this.levelList = data
			this.levelObj = data.map(item=>{
				let temp = {}
				temp[item.id] = item.name
				return temp
			})
		})
	},
	activated () {
		
	},
	mounted () {
		
	}
}
</script>
